const app = getApp();
const utils = require('../../utils/util.js');
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    currPagePath: {
      type: String,
      value: '/pages/index/index'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    color: '#8a8a8a',
    selectedColor: '#FB8524',
    TabbarList: []
  },
  

  ready: function() {
    var timer = setInterval(function (app, page) {
      console.log('timer')
      if (app.globalData.userInfo) {
        page.getStaffTabbars();
        console.log('timer end')
        clearInterval(timer);
      }
    }, 1000, app, this)
  },
  /**
   * 组件的方法列表
   */
  methods: {
    _initTabBarPages: function() {
      var that = this;
      var newPages = that.data.TabbarList;
      var newTabbar = [];
      newPages.forEach((item) => {
        item.isActive = item.pagePath == that.data.currPagePath;
        if (item.display) {
          newTabbar.push(item)
        }
      })
      that.setData({
        TabbarList: newTabbar
      })
    },
    // 单击tab触发的函数
    onTabbarItemTap(ev) {
      console.log(ev);
      // let key = ev.currentTarget.dataset.key;
      let toUrl = ev.currentTarget.dataset.tourl;
      utils.subscribe();
      wx.reLaunch({
        url: toUrl,
      })
    },

    formSubmitAuthori: function(ev){
      var formId = ev.detail.formId;
      utils.addFormIdMsg(formId);
    },
    //获取名片底部导航
    getStaffTabbars: function() {
      var that = this;
      var cardId = wx.getStorageSync('cardId');
      var TabbarList = [{
          id: 0,
          pagePath: '/pages/index/index',
          text: 'AI名片',
          iconPath: '/images/tabbar/20.png',
          selectedIconPath: '/images/tabbar/19.png',
          iCount: 0,
          isActive: true,
          target: 'relaunch',
          display: true
        }, {
          id: 1,
          pagePath: '/pages/weijieke/states/index',
          text: '商圈',
          iconPath: '/images/tabbar/24.png',
          selectedIconPath: '/images/tabbar/23.png',
          iCount: 0,
          isActive: false,
          target: 'relaunch',
          display: true
        },
        {
          id: 2,
          pagePath: '/pages/classes-new/index',
          text: '产品',
          iconPath: '/images/tabbar/22.png',
          selectedIconPath: '/images/tabbar/21.png',
          iCount: 0,
          isActive: false,
          target: 'relaunch',
          display: true
        },
        {
          id: 3,
          pagePath: '/pages/about/index',
          text: '官网',
          iconPath: '/images/tabbar/26.png',
          selectedIconPath: '/images/tabbar/25.png',
          iCount: 0,
          isActive: false,
          target: 'relaunch',
          display: true
        }
      ]
      wx.request({
        url: app.globalData.host + '/api/AddressBook/GetStaffTabbars?staffId=' + cardId + '&uid=' + app.globalData.userInfo.Uid,
        success: function(res) {
          if (res.data.Code == 0) {
            var list = res.data.Data;
            var showName = "";
            for (var i = 0; i < list.length; i++) {
              if (list[i].UserName) {
                showName = list[i].UserName;
              } else {
                showName = list[i].Name;
              }
              TabbarList[i].display = list[i].Display;
              TabbarList[i].text = showName;
            }
            that.setData({
              TabbarList: TabbarList
            })
            that._initTabBarPages();
          }
        },
      })
    }
  }
})